package com.speech.utils;

import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by XuLH on 2018/6/21.
 */
public class AESEncryption {
    final static char[] chars = "0123456789ABCDEF".toCharArray();

//    private static byte[] key = {
//            0x78, 0x62, 0x74, 0x64, 0x79, 0x73, 0x40, 0x77, 0x6a, 0x71, 0x77, 0x20, 0x45, 0x29, 0x40, 0x40
//    };

    private static byte[] key = "bGluYVNwZWFrZXIK".getBytes();

    protected static SecretKeySpec secretKey = new SecretKeySpec(key, "AES");

    protected static String ByteArray2HexStr(byte[] barr) {
        StringBuilder sb = new StringBuilder();
        int bit;
        for (int i = 0; i < barr.length; i++) {
            bit = (barr[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = barr[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }

    protected static int ToByte(char c) throws Exception {
        if (c >= '0' && c <= '9')
            return c - '0';
        else if (c >= 'A' && c <= 'F')
            return c - '7';
        else if (c >= 'a' && c <= 'f')
            return c - 'W';

        throw new Exception();
    }

    protected static byte[] HexStr2ByteArray(String hex) throws Exception {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (ToByte(achar[pos]) << 4 | ToByte(achar[pos + 1]));
        }
        return result;
    }

    protected static String Base64Encode(byte[] barr) {
        return Base64.encodeToString(barr, Base64.NO_WRAP);
    }

    protected static byte[] Base64Decode(String str) {
        return Base64.decode(str, Base64.DEFAULT);
    }

    public static String encrypt(String plainText) {
        if (plainText == null || plainText.length() == 0)
            return null;
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] cipherText = cipher.doFinal(plainText.getBytes("UTF8"));
            String encryptedString = Base64Encode(cipherText);
            return encryptedString;
        } catch (Exception e) {
        }

        return null;
    }

    public static String decrypt(String encryptedText) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            String decryptedString = new String(cipher.doFinal(Base64Decode(encryptedText)), "UTF-8");
            return decryptedString;
        } catch (Exception e) {
        }

        return null;
    }

    public static void main(String args[]) {
//        String encrypted = encrypt("BuildacodeAES-128bitEncryption");
//        String decrypted = decrypt("3Srch9X7Uat+cnIpRaezqMkNndJKc1YOhUhz51otlhI=");
//        System.out.println("Encrypted String : " + encrypted);
//        System.out.println("Decrypted String : " + decrypted);

        String encrypted = encrypt("BuildacodeAES-128bitEncryption");
        System.out.println("Encrypted String : " + encrypted);
//        System.out.println("Decrypted String : " + decrypted);




    }
}
